"""GitHub user page on Gittip. """ import decimal import os import requests from aspen import json, Response from gittip import AMOUNTS, CARDINALS, db from gittip.elsewhere import github from gittip.models import Participant # ========================================================================== ^L # Try to load from GitHub. # ======================== user_info = github.get_user_info(path['login']) # Try to load from Gittip. # ========================== # We can only tip Users, not Organizations (or whatever else type can be). username = user_info['login'] name = user_info.get('name') if not name: name = username usertype = user_info.get("type", "unknown type of account").lower() if usertype == "user": account = github.GitHubAccount(user_info['id'], user_info) locked = account.is_locked lock_action = "unlock" if account.is_locked else "lock" if account.is_claimed: request.redirect('/%s/' % account.participant) participant = Participant.query.get(account.participant) if not user.ANON: my_tip = user.get_tip_to(participant.username) tip_or_pledge = "pledge" number = participant.get_number_of_backers() img_src = participant.get_img_src(128) elif usertype == "organization": members = [] resp = requests.get( "https://api.github.com/orgs/%s/members" % username, params={'client_id': os.environ['GITHUB_CLIENT_ID'], 'client_secret': os.environ['GITHUB_CLIENT_SECRET']} ) if resp.status_code == 200: members = json.loads(resp.text) logins = [member['login'] for member in members] ON_GITTIP = """\ SELECT participant, elsewhere.user_info -> 'login' AS login FROM elsewhere JOIN participants p ON p.username = elsewhere.participant WHERE elsewhere.platform = 'github' AND elsewhere.user_info -> 'login' = any(%s) AND p.claimed_time IS NOT NULL """ known = list(db.fetchall(ON_GITTIP, (logins,))) known_map = {rec['login']: rec['participant'] for rec in known} _on_gittip = [rec['login'] for rec in known] on_gittip = [] not_on_gittip = [] for member in members: if member['login'] in _on_gittip: seq = on_gittip tippee = known_map[member['login']] else: seq = not_on_gittip elsewhere = github.GitHubAccount(member['id'], member) tippee = elsewhere.participant tip_to = None if user.ANON else user.get_tip_to(tippee) seq.append((member, tippee, tip_to)) can_tip = user.ANON number = len(members) img_src = user_info['avatar_url'] + "&s=128" else: can_tip = False img_src = "/assets/-/avatar-default.gif" url = user_info['html_url'] title = username # ========================================================================== ^L {% extends templates/base.html %} {% block heading %}

GitHub

{% end %} {% block box %}

{{ escape(username) }} has

{{ number }}
{% if usertype == "user" %}
{{ 'person' if number == 1 else 'people' }} ready to give
{% elif usertype == "organization" %}
public member{{ '' if number == 1 else 's' }}
{% end %}
{% if user.ANON or usertype == "user" %} {% end %} {% end %} {% block page %} {% if usertype == "user" %}
{% if account.is_locked %}

{{ escape(username) }} has opted out of Gittip.

If you are {{ escape(username) }} on GitHub, you can unlock your account to allow people to pledge tips to you on Gittip.

{% else %}

{{ escape(name) }} has not joined Gittip.

Is this you? {% if user.ANON %} Click here to opt in to Gittip. We never collect money for you until you do. {% else %} Sign out and sign back in to claim this account {% end %}

{% if user.ANON %}

What is Gittip?

Gittip is a way to thank and support your favorite artists, musicians, writers, programmers, etc. by setting up a small weekly cash gift to them. Read more ...

Don't like what you see?

If you are {{ escape(username) }} you can explicitly opt out of Gittip by locking this account. We don't allow new pledges to locked accounts.

{% end %} {% end %}
{% elif usertype == "organization" %} {% for i, sequence in enumerate([on_gittip, not_on_gittip]) %} {% set nsequence = len(sequence) %} {% if sequence %} {% end %} {% for member, tippee, tip in sequence %} {% else %} {% end %} {% end %} {% end %}

{{ nsequence }} {% if number > 0 %} ({{ "%.1f" % (nsequence * 100 / float(number)) }}%) {% end %} {{ 'is' if nsequence == 1 else 'are' }} {{ i == 0 and "also on" or "not on" }} Gittip

{% if not user.ANON %} {{ member['login'] }} {% for amount in AMOUNTS %} {% end %} {% if tip not in AMOUNTS %} old amount! {% end %} {{ member['login'] }}
{% else %}

Not sure what to do with {{ name }}.

I don't recognize the “{{ usertype }}” type of user on GitHub.
Sorry. :-( {% end %} {% end %}